package com.cby.operation.pay.data

import com.cby.operation.common.http.HttpHelper
import com.cby.operation.common.util.CbyLogUtils
import com.cby.operation.common.util.DateUtils
import com.cby.operation.common.util.ThreadPoolUtils
import com.cby.operation.sdk.*
import com.cby.operation.sdk.http.UrlHelper
import java.util.HashMap

object PayLogApiRepository {

    private const val TAG = "PayLog"

    private const val PAY_NOTIFY_PATH = "pay/notify"

    private const val VALUE_PAY_CALLBACK = "unity callback"

    /**
     * 支付信息上报
     */
    private val BASE_PAY_NOTIFY_URL = UrlHelper.buildSdkUrl(PAY_NOTIFY_PATH)

    /**
     * 游戏方上报商品发放通知
     */
    private const val BASE_NOTIFY_PAY_LOG = "http://sdk.3839app.com/pay/log"

    /**
     * 支付结果上报日志
     *
     * @param orderId 游戏方的商品订单号
     * @param content 支付结果
     */
    fun payReport(orderId: String, content: String) {
        val param: MutableMap<String, Any> = HashMap()
        param[KEY_GAME_ID] = SdkConfiguration.gameId
        param[KEY_PAY_ORDER] = orderId
        param[KEY_CONTENT] = content
        param[KEY_VERSION] = SdkConfiguration.sdkVersion
        HttpHelper.requestPost(
            BASE_PAY_NOTIFY_URL, param,
            UrlHelper.authHeaders,
            null)
    }

    /**
     * 发放商品上报日志
     *
     * @param cpOrderId String  游戏方的商品订单号
     * @param money String  下单金额
     * @param goodsName String  商品名称
     */
    fun notifyGoods(cpOrderId: String, money: String, goodsName: String) {
        val param: MutableMap<String, Any> = HashMap()
        param[KEY_PAY_GAME_ORDER] = cpOrderId
        param[KEY_PAY_MONEY] = money
        param[KEY_PAY_GOODS_NAME] = goodsName
        param[KEY_GAME_ID] = SdkConfiguration.gameId
        param[KEY_VERSION] = SdkConfiguration.sdkVersion
        param[KEY_TIME_STAMP] = DateUtils.getCurrentTime()
        ThreadPoolUtils.execute(Runnable {
            HttpHelper.requestPost(
                BASE_NOTIFY_PAY_LOG, param,
                UrlHelper.authHeaders,
                null)
        })
    }

    /**
     * unity的支付回调上报日志
     *
     * @param cpOrderId
     */
    fun unityCallBackReport(cpOrderId: String) {
        val param: MutableMap<String, Any> = HashMap()

        param[KEY_PAY_CALLBACK] = VALUE_PAY_CALLBACK
        param[KEY_PAY_GAME_ORDER] = cpOrderId
        param[KEY_GAME_ID] = SdkConfiguration.gameId
        param[KEY_TIME_STAMP] = DateUtils.getCurrentTime()
        param[KEY_VERSION] = SdkConfiguration.sdkVersion
        ThreadPoolUtils.execute(Runnable {
            HttpHelper.requestPost(
                BASE_NOTIFY_PAY_LOG, param,
                UrlHelper.authHeaders,
                null)
        })
    }
}